<script setup>
import { onMounted, ref } from 'vue'
import {
  ScenicAreaSearchService,
  ScenicAreaDelService,
  AuditScenicAreaService
} from '@/api/scenicArea'
import { Delete, Edit, Close, Check } from '@element-plus/icons-vue'
import { formatTime } from '@/utils/format.js'
import ScenicAreaEdit from './component/ScenicAreaEdit.vue'
import { useUserStore } from '@/stores/modules/user'

const userStore = useUserStore()
const role = userStore.userRole
const ScenicAreaListRef = ref()
const defaultParams = ref({
  id: null,
  pageNum: 1,
  pageSize: 10,
  scenicAreaLatitude: '',
  scenicAreaLongitude: '',
  scenicAreaName: '',
  scenicAreaInfo: '',
  scenicAreaVoice: '',
  createScenicAreaUserAccount: '',
  scenicAreaStatus: null
})
const params = ref({
  id: null,
  pageNum: 1,
  pageSize: 10,
  scenicAreaLatitude: '',
  scenicAreaLongitude: '',
  scenicAreaName: '',
  scenicAreaInfo: '',
  scenicAreaVoice: '',
  createScenicAreaUserAccount: '',
  scenicAreaStatus: null
})
const total = ref(0)

const isloading = ref(false)
const ScenicList = ref([])

const getScenicAreaList = async () => {
  isloading.value = true
  const res = await ScenicAreaSearchService(params.value)
  console.log(res.data.data.records)
  ScenicList.value = res.data.data.records
  total.value = res.data.data.total // 赋值 total
}
const onEdit = (row) => {
  console.log(row)
  console.log('编辑', row.id)

  ScenicAreaListRef.value.open(row)
}
const onAddSpot = () => {
  console.log('添加')
  ScenicAreaListRef.value.open({})
}
const onDelete = async (row) => {
  console.log('删除', row)
  let ids = row.id
  console.log('Delete article', ids)

  await ElMessageBox.confirm('是否要删除', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  if (!Array.isArray(ids)) {
    ids = [ids]
  }

  await ScenicAreaDelService(ids)
  ElMessage.success('删除成功')
  getScenicAreaList()
  // 实现删除逻辑
}

// 搜索操作
const onSearch = async () => {
  try {
    isloading.value = true
    const res = await ScenicAreaSearchService({ ...params.value })
    if (res && res.data && res.data.data && res.data.data.records) {
      ScenicList.value = res.data.data.records
    } else {
      ScenicList.value = [] // 如果没有结果，清空列表
    }
  } catch (error) {
    console.error('搜索失败:', error)
  } finally {
    isloading.value = false
  }
}
// 重置

const onReset = () => {
  params.value = { ...defaultParams }
  params.value.pageNum = 1 // 重置页面
  getScenicAreaList()
}
onMounted(getScenicAreaList)
const onSuccess = () => {
  const lastPage = Math.ceil((total.value + 1) / params.value.pageSize)
  // 更新成最大页码数，再渲染
  params.value.pageNum = lastPage
  getScenicAreaList()
}
// 处理分页
const onSizeChange = (size) => {
  params.value.pageNum = 1
  params.value.pageSize = size
  // 基于最新的当前页 和 每页条数，渲染数据
  getScenicAreaList()
}
const onCurrentChange = (page) => {
  // 更新当前页
  params.value.pageNum = page
  // 基于最新的当前页，渲染数据
  getScenicAreaList()
}

// 审核
const OnAudit = async (row) => {
  console.log(row)
  let id = row.id
  const params = new URLSearchParams()
  params.append('id', id)
  console.log(params)
  const res = await AuditScenicAreaService(params)
  // console.log('res', res)
  if (res.data.code == 200) {
    ElMessage.success('审核成功')
    getScenicAreaList()
  } else {
    ElMessage.error('审核失败')
  }
}

onMounted(getScenicAreaList)
</script>
<template>
  <page-container title="景区管理">
    <template #extra>
      <el-button type="primary" @click="onAddSpot">添加景区</el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline>
      <el-form-item label="景区名称">
        <el-input
          placeholder="请输入景区名称"
          clearable
          v-model="params.scenicAreaName"
        />
      </el-form-item>
      <el-form-item label="审核状态" style="width: 200px">
        <el-select
          v-model="params.scenicAreaStatus"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option label="未审核" value="0"></el-option>
          <el-option label="已审核" value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="ScenicList" v-model="isloading">
      <el-table-column
        label="序号"
        width="120"
        type="index"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="景区名称"
        prop="scenicAreaName"
        width="240"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="景区介绍"
        prop="scenicAreaInfo"
        width="240"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-tooltip effect="light" placement="top">
            <template #content>
              <div class="tooltip-content">{{ row.scenicAreaInfo }}</div>
            </template>
            <div class="ellipsis-tooltip" :style="{ maxWidth: '300px' }">
              {{ row.scenicAreaInfo }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        label="图片"
        prop="scenicAreaImages"
        width="240"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <!-- <el-button type="primary" @click="playAudio(row.scenicAreaVoice)"
            >语音</el-button
          > -->
          <img :src="row.scenicAreaImages[0]" alt="" class="_img" />
        </template>
      </el-table-column>

      <el-table-column
        label="创建人"
        prop="createScenicAreaUserAccount"
        width="240"
        header-align="center"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        width="220"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          {{ formatTime(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column
        label="审核状态"
        prop="scenicAreaStatus"
        width="100px"
        header-align="center"
        align="center"
      >
      </el-table-column>

      <el-table-column
        label="操作"
        width="220px"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEdit(row)"
          ></el-button>

          <el-button
            type="primary"
            :icon="Close"
            circle
            plain
            v-if="row.scenicAreaStatus == 0 && role === 1"
            @click="OnAudit(row, $index)"
          ></el-button>
          <el-button
            type="primary"
            v-if="row.scenicAreaStatus == 1 && role === 1"
            :icon="Check"
            circle
          ></el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelete(row)"
          ></el-button> </template
      ></el-table-column>

      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <el-pagination
      v-model:current-page="params.pageNum"
      v-model:page-size="params.pageSize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />

    <scenicArea-edit
      ref="ScenicAreaListRef"
      @success="onSuccess"
    ></scenicArea-edit>
  </page-container>
</template>
<style lang="scss" scoped>
._img {
  width: 50px;
}
</style>
<style lang="css">
.ellipsis-tooltip {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1; /* 设置显示的行数 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  cursor: pointer;
}
.tooltip-content {
  max-width: 300px; /* 工具提示的最大宽度 */
  white-space: normal;
  word-break: break-all;
}
</style>
